Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: TF_CLI_ARGS_* Handling #898

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open

Conversation

milldr
Copy link
Member

@milldr milldr commented Dec 27, 2024

what

  • Added logic and warning messages when the user specifies any TF_CLI_ARGS_* environment variable for -var-file, since this may conflict with Atmos generated var files.
  • Added to 3 places: terraform shell, terraform as a ENV var, terraform as a component env var

why

  • When executing Terraform Shell, we should append generate var file to the (if) specified var file rather than overwriting it
  • When executing Terraform, var files are already appended (merged). We should add a warning nonetheless

references

2024-12-27 10 13 41

Summary by CodeRabbit

  • New Features

    • Enhanced logging to alert users about potential conflicts with Terraform environment variables.
    • Improved handling of -var-file arguments to respect existing configurations.
  • Bug Fixes

    • Enhanced management of Terraform command-line arguments to prevent unintentional overwriting of variable files.

@mergify mergify bot added the triage Needs triage label Dec 27, 2024
@milldr milldr added minor New features that do not break anything and removed triage Needs triage labels Dec 27, 2024
@milldr milldr marked this pull request as ready for review December 27, 2024 15:15
@milldr milldr requested a review from a team as a code owner December 27, 2024 15:15
Copy link
Contributor

coderabbitai bot commented Dec 27, 2024

📝 Walkthrough

Walkthrough

The pull request enhances the handling of Terraform command-line arguments and environment variables within the Atmos shell utility functions. It introduces a structured approach for managing -var-file arguments and adds warning mechanisms to prevent unintentional overwriting of existing environment variables. Additionally, it improves logging by checking for pre-existing Terraform-related environment variables and notifying users about potential conflicts.

Changes

File Change Summary
internal/exec/shell_utils.go Modified execTerraformShellCommand to implement structured handling of Terraform command-line arguments.
internal/exec/terraform.go Added checks and warnings for existing environment variables starting with TF_.

Assessment against linked issues

Objective Addressed Explanation
Warn about TF_CLI_ env variable replacement [DEV-2793]
Append to existing environment variables instead of replacing

Possibly related PRs

Suggested reviewers

  • Gowiem
  • aknysh
  • osterman

📜 Recent review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 0cbee45 and 971be07.

📒 Files selected for processing (2)
  • internal/exec/shell_utils.go (2 hunks)
  • internal/exec/terraform.go (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • internal/exec/terraform.go
  • internal/exec/shell_utils.go

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary or @auto-summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai or @auto-title anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 0

🧹 Nitpick comments (1)
internal/exec/terraform.go (1)

262-264: Catching var-file collisions in component environment ensures thorough coverage.
These lines do a great job of warning users when environment-level settings might be overridden.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1aa75c6 and 42c3026.

📒 Files selected for processing (2)
  • internal/exec/shell_utils.go (1 hunks)
  • internal/exec/terraform.go (1 hunks)
🔇 Additional comments (4)
internal/exec/shell_utils.go (3)

188-190: Neat definition of terraform commands.
This approach centralizes the commands that may require var-file handling, simplifying future maintenance and extension.


191-197: Clear warning message for potential collisions.
It is helpful to log a warning whenever these TF_CLI_ARGS variables contain a “-var-file” argument, reducing user confusion.


198-200: Appending the var file reference to each command is straightforward.
Ensure users understand that this logic overrides any existing “-var-file” specification at runtime.

internal/exec/terraform.go (1)

248-257: Collisions detection is consistent with shell_utils.
Capturing conflicts across both the shell and terraform commands maintains consistent user visibility.

coderabbitai[bot]
coderabbitai bot previously approved these changes Dec 27, 2024
@osterman
Copy link
Member

osterman commented Dec 30, 2024

Here's the misunderstanding.

  1. execTerraformShellCommand is only called for atmos terraform shell (in your screenshots you're running other commands)
  2. The 5th argument is the var file.
  3. When running atmos terraform shell, it will squash the TF_CLI_ARG_* environment variables. Not just when we're passing a -var-file. E.g. Setting TF_CLI_ARG_plan=-non-existant-flag will break Terraform. Atmos warning that the ENV is set, will alert the user during debugging.

Here are the new requirements

  1. Anytime we execute terraform we should probably warn emit a warning if any of these variables are set, as it can change the deterministic behavior of atmos
  2. Modify execTerraformShellCommand so that it does not squash the values of TF_CLI_ARG_* and instead appends (with a space) to it.

for _, cmd := range tfCommands {
envVar := fmt.Sprintf("TF_CLI_ARGS_%s", cmd)
existing := os.Getenv(envVar)
if existing != "" && strings.Contains(existing, "-var-file=") {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

replace this behavior. Check for any "TF_" prefixed env var and give a warning. Do not emit the value, but do emit the name of the variable

u.LogWarning(atmosConfig, "Found var-file in environment! This may be overwritten by Atmos")
}
// Set the Terraform environment variable to reference the var file
componentEnvList = append(componentEnvList, fmt.Sprintf("%s=-var-file=%s", envVar, varFile))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead append Atmos settings to any TF_CLI_ARGS_*. Atmos takes precedence.

Comment on lines 262 to 264
if strings.Contains(v, "-var-file=") {
u.LogWarning(atmosConfig, "Found var-file in component environment! This may be overwritten by Atmos")
}
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Confirm this isnt processed twice. We should already have merged the env vars before running this function altogether

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Replace with if the value starts with TF_CLI, print warning message

@milldr milldr changed the title feat: TF_CLI_ARGS_* Handling for -var-file feat: TF_CLI_ARGS_* Handling Jan 2, 2025
coderabbitai[bot]
coderabbitai bot previously approved these changes Jan 2, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
minor New features that do not break anything
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants